package secondkill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 所有传输消息都经过RabbitMQ中队列 交换机与队列交换消息通过路由键匹配
 * 四种交换机模式 direct直接 topic主题 headers头部
 * fanout广播(生产者的消息会被发往不同的路由器接收)
 * @Auther  丁子川
 * @create 2022/1/19 13:03
 */

//@Configuration
public class RabbitMQConfig {

    //Fanout模式
    private static final String QUEUE_1 = "Queue_Fanout01";
    private static final String QUEUE_2 = "Queue_Fanout02";
    private static final String EXCHANGE_1 = "Fanout_Exchange";

    @Bean
    //配置队列
    public Queue queue_1() {
        return new Queue(QUEUE_1);
    }
    public Queue queue_2() {
        return new Queue(QUEUE_2);
    }

    @Bean
    //配置交换机
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(EXCHANGE_1);
    }

    @Bean
    //队列绑定交换机
    public Binding binding_1() {
        return BindingBuilder.bind(queue_1()).to(fanoutExchange());
    }
    public Binding binding_2() {
        return BindingBuilder.bind(queue_2()).to(fanoutExchange());
    }

    //Direct模式
    private static final String QUEUE_3 = "Queue_Direct01";
    private static final String QUEUE_4 = "Queue_Direct02";
    private static final String EXCHANGE_2 = "Direct_Exchange";
    private static final String ROUTINGKEY_1 = "Direct_Key01";
    private static final String ROUTINGKEY_2 = "Direct_Key02";

    @Bean
    public Queue queue_3() {
        return new Queue(QUEUE_3);
    }
    public Queue queue_4() {
        return new Queue(QUEUE_4);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_2);
    }

    @Bean
    public Binding binding_3() {
        return BindingBuilder.bind(queue_3()).to(directExchange()).with(ROUTINGKEY_1);
    }
    public Binding binding_4() {
        return BindingBuilder.bind(queue_4()).to(directExchange()).with(ROUTINGKEY_2);
    }

    //Topic模式
    private static final String QUEUE_5 = "Queue_Topic01";
    private static final String QUEUE_6 = "Queue_Topic02";
    private static final String EXCHANGE_3 = "Topic_Exchange";
    // * 表示精确匹配一个 # 表示匹配任意
    private static final String ROUTINGKEY_3 = "#.key.#";
    private static final String ROUTINGKEY_4 = "*.key.#";

    @Bean
    public Queue queue_5() {
        return new Queue(QUEUE_5);
    }
    public Queue queue_6() {
        return new Queue(QUEUE_6);
    }

    @Bean
    public TopicExchange topicExchange01() {return new TopicExchange(EXCHANGE_3);}

    @Bean
    public Binding binding_5() {
        return BindingBuilder.bind(queue_5()).to(topicExchange01()).with(ROUTINGKEY_3);
    }
    public Binding binding_6() {
        return BindingBuilder.bind(queue_6()).to(topicExchange01()).with(ROUTINGKEY_4);
    }

    //Headers模式
    private static final String QUEUE_7 = "Queue_Headers01";
    private static final String QUEUE_8 = "Queue_Headers02";
    private static final String EXCHANGE_4 = "Headers_Exchange";

    @Bean
    public Queue queue_7() {
        return new Queue(QUEUE_7);
    }
    public Queue queue_8() {
        return new Queue(QUEUE_8);
    }

    @Bean
    public HeadersExchange headersExchange() {return new HeadersExchange(EXCHANGE_4);}

    @Bean
    public Binding binding_7() {
        Map<String, Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","low");
        //发送信息头部必须添加map中key的任意一个
        return BindingBuilder.bind(queue_7()).to(headersExchange()).whereAny(map).match();
    }
    public Binding binding_8() {
        Map<String, Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","fast");
        //发送信息头部必须添加map中key的所有值
        return BindingBuilder.bind(queue_8()).to(headersExchange()).whereAll(map).match();
    }
}
